package com.sakila.service.util;

import java.util.Collections;
import java.util.List;

import org.apache.commons.beanutils.BeanComparator;
import org.apache.commons.collections.comparators.ComparableComparator;
import org.apache.commons.collections.comparators.ReverseComparator;
import org.apache.commons.lang3.text.WordUtils;


/**
 * {@link reference: https://commons.apache.org/proper/commons-beanutils/apidocs/org/apache/commons/beanutils/package-summary.html#FAQ.bc.order}
 * @author nerine
 *
 */
public class SortListUtil {
	
	public static final <T> void sort(List<T> list, String order, String sortField){
		//String property = "get" + WordUtils.capitalize(sortField);
		BeanComparator<T> bc;
		if(order.equals("desc")){
			bc = new BeanComparator<T>(sortField, new ReverseComparator(new ComparableComparator()));
		}else {
			bc = new BeanComparator<T>(sortField);
		}
		Collections.sort(list, bc);
	}
}
